package com.caigou.comm.base.utils

import android.text.TextUtils
import com.caigou.comm.base.bean.Session
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2021/10/10
 */
object SessionUtil {
    @Synchronized
    fun getSession(): Session? {
        val json = Prefs.instance.userKV.decodeString("session", "")
        return if (TextUtils.isEmpty(json)) {
            null
        } else {
            val jsonObject = JSONObject(json)
            Session(jsonObject.getString("mobile"), jsonObject.getString("password"))
        }
    }

    @Synchronized
    fun setSession(session: Session) {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            jsonObject.put("mobile", session.mobile)
            jsonObject.put("password", session.password)
        }
        Prefs.instance.userKV.encode("session", jsonObject.toString())
    }
}
